/**
 * 广寒宫
 * 网址:www.guanghangong.xyz
 */
package com.moon.admin.sys.controller;

import com.moon.admin.sys.cache.ConfigCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.Api;

import com.moon.admin.sys.entity.SysConfigEntity;
import com.moon.admin.sys.domain.query.ConfigQuery;
import com.moon.admin.sys.service.SysConfigService;
import com.moon.admin.framework.util.ToolUtils;
import com.moon.admin.framework.secure.annotation.FormRepeat;
import com.moon.admin.framework.secure.annotation.PreAuth;
import com.moon.admin.framework.springmvc.domain.R;
import com.moon.admin.framework.syslog.annotation.SysLog;
import com.moon.admin.framework.domain.MoonPage;
import com.moon.admin.framework.mybatisplus.query.Condition;
import springfox.documentation.annotations.ApiIgnore;

/**
 * 系统参数 操作处理
 * 
 * @author moon
 */
@RestController
@RequestMapping("/config/sysConfig")
@Api(value="系统参数",tags="系统参数服务")
public class SysConfigController{
	
    @Autowired
    private SysConfigService sysConfigService;

    @ApiOperation(value="系统参数列表")
    @PostMapping(value = "list")
    @PreAuth("config_sysConfig_view")
    public R<MoonPage<SysConfigEntity>> list(@RequestBody ConfigQuery sysConfig) {
        IPage<SysConfigEntity> pages = sysConfigService.page(Condition.getPage(sysConfig),Condition.getQueryWrapper(new SysConfigEntity(),sysConfig));
    	return R.success(MoonPage.of(pages));
    }

    @ApiOperation(value="系统参数添加/编辑")
    @PostMapping(value = "editSave")
    @PreAuth(value={"config_sysConfig_add","config_sysConfig_edit"})
    @SysLog(mod="系统参数服务",fun="保存")
    @FormRepeat
    public R<String> editSave(@RequestBody SysConfigEntity sysConfig){
        sysConfigService.saveOrUpdate(sysConfig);
        return R.success();
    }

    @GetMapping("/removeByIds")
    @ApiOperation(value = "系统参数", notes = "传入ids")
    @PreAuth("config_sysConfig_remove")
    @SysLog(mod="系统参数服务",fun="删除")
    @FormRepeat
    public R<Boolean> removeByIds(@ApiParam(value = "主键集合", required = true)String ids) {
        return R.status(sysConfigService.removeByIds(ToolUtils.toIntList(ids)));
    }
    @ApiOperation(value="修改系统配置状态")
    @GetMapping("/updateStatusById")
    public R<Void> updateStatusById(Integer id,Integer status){
        SysConfigEntity entity = sysConfigService.getById(id);
        entity.setStatus(status);
        sysConfigService.updateById(entity);
        ConfigCache.cleanCache();
        return R.success();
    }

    @GetMapping("/cleanCache")
    @ApiIgnore
    public R<Void> cleanCache(){
        ConfigCache.cleanCache();
        return R.success();
    }
}